package cn.yedong.org.service.impl;

import cn.yedong.basic.service.impl.BaseServiceImpl;
import cn.yedong.basic.util.Md5Utils;
import cn.yedong.basic.util.StrUtils;
import cn.yedong.org.domain.Employee;
import cn.yedong.org.mapper.EmployeeMapper;
import cn.yedong.org.service.IEmployeeService;
import cn.yedong.user.domain.Logininfo;
import cn.yedong.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务实现类：部门
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 添加
     * @param employee
     */
    @Transactional
    @Override
    public void add(Employee employee) {
        //处理密码和盐值
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //先添加logininfo-产生自增id
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);//产生自增id

        //保存employee
        employee.setLogininfo_id(logininfo.getId());
        super.add(employee);

        //添加角色
        if(employee.getRole_id() != null){
            employeeMapper.addRole(employee);
        }
    }

    /**
     * 删除
     * @param id
     */
    @Transactional
    @Override
    public void del(Long id) {
        //先根据employee的logininfoId删除logininfo
        Employee employee = employeeMapper.loadById(id);
        logininfoMapper.del(employee.getLogininfo_id());

        //删除角色
        employeeMapper.delRoleByEmpLoyeeId(id);

        //再删除employee
        super.del(id);
    }

    /**
     * 修改
     * @param employee
     */
    @Transactional
    @Override
    public void update(Employee employee) {
        //获取数据库中employee
        Employee dbEmployee = employeeMapper.loadById(employee.getId());
        //如果修改的密码，与数据库中密码不一致，则不处理密码
        if(!employee.getPassword().equals(dbEmployee.getPassword())){
            //处理密码和盐值
            String salt = StrUtils.getComplexRandomString(32);
            String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
            employee.setSalt(salt);
            employee.setPassword(md5Pwd);
        }
//        else{   这个时候传过来有盐值
//            employee.setSalt(dbEmployee.getSalt());
//        }
        //将dbEmployee原先的Logininfo_id设置回去
        employee.setLogininfo_id(dbEmployee.getLogininfo_id());
        //将dbEmployee原先的ShopId设置回去
        employee.setShop_id(dbEmployee.getShop_id());
        //修改employee
        super.update(employee);
        //获取数据库中Logininfo
        Logininfo dbLogininfo = logininfoMapper.loadById(dbEmployee.getLogininfo_id());
        //拷贝属性
        BeanUtils.copyProperties(employee,dbLogininfo);
        //注意：属性拷贝的时候dbLogininfo的id会被覆盖，所以需要设置会原来的id
        dbLogininfo.setId(dbEmployee.getLogininfo_id());
        //判断员工修改的状态-修改dbLogininfo状态
        if(employee.getState()==1){
            dbLogininfo.setDisable(true);
        }else {
            dbLogininfo.setDisable(false);
        }
        //修改Logininfo
        logininfoMapper.update(dbLogininfo);
        //删除角色
        employeeMapper.delRoleByEmpLoyeeId(employee.getId());
        //添加角色
        if(employee.getRole_id() != null){
            employeeMapper.addRole(employee);
        }
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    @Override
    public void patchDel(List<Long> ids) {
        List<Long> logininfoIds = new ArrayList<>();
        for (Long id: ids) {
            Employee employee = employeeMapper.loadById(id);
            Long logininfoId = employee.getLogininfo_id();
            //找到所有的登录信息
            logininfoIds.add(logininfoId);
            //this.del(id);//调用当前类id方法删除


        }
        //根据员工id批量删除角色
        employeeMapper.patdelRoleByEmpLoyeeIds(ids);
        //根据员工id批量删除员工登陆信息
        logininfoMapper.patchDel(logininfoIds);
        super.patchDel(ids);
    }

    /**
     * 获取指定店铺全部员工
     * @param shioId
     * @return
     */
    @Override
    public List<Employee> findByShopId(Long shioId) {
        return employeeMapper.findByShopId(shioId);
    }
}
